const Router = require('koa-router');
const router = new Router();
const tool = require('../../tools/tool')
const {db,  Op} = require("../../models/db");

router.post('/goods/save', async ctx => {
    try {
        let tid = ctx.user.tenantId;
        let uid = ctx.user.id;
        let formData = ctx.request.body.formData;
        let where = {
            tenantId: tid,
            name: formData.name
        }
        if(formData.id) {
            where.id = {
                [Op.ne]: formData.id
            }
            formData.uby = uid
            formData.utime = tool.getTime()
        }
        if(tool.xe.isNaN(Number(formData.spec))) {
            return ctx.err('规格输入整数')
        }
        let c = await db.models['goods'].count({
            where
        })
        if(c > 0) {
            return ctx.err('已存在相同的商品名称')
        }
        if(formData.unit1 === formData.unit2) {
            return ctx.err('大单位不能等于小单位')
        }
        if(formData.spec < 1) {
            return ctx.err('规格请输入大于0的整数')
        }

        formData.pinYin = tool.py(formData.name)
        let u1 = tool.xe.trim(formData.unit1);
        let u2 = tool.xe.trim(formData.unit2);
        let u1m;
        let u2m;
        if(u1) {
            u1m = await db.models['unit'].findOne({
                where: {
                    name: u1
                }
            })
            if(!u1m) return ctx.err('未找到该单位')
        }
        if(u2) {
            u2m = await db.models['unit'].findOne({
                where: {
                    name: u2
                }
            })
            if(!u2m) return ctx.err('未找到该单位')
        }
        if(formData.id) {
            let model = await db.models['goods'].findOne({
                where: {
                    tenantId: tid,
                    id: formData.id
                }
            })
            let ifUsed = false
            if(!model) return ctx.err('查询商品失败')
            let used = await db.models['invoiceInfo'].count({
                where: {
                    invId: formData.id
                }
            })

            if (used > 0)
                ifUsed = true;
            let usedInit = await db.models['initGoods'].count({
                where: {
                    invId: formData.id
                }
            })
            if (usedInit > 0)
                ifUsed  = true;

            if(tool.xe.toNumber(model.spec) !== tool.xe.toNumber(formData.spec) && ifUsed) {
                return ctx.err('已使用商品，不能再修改规格')
            }


            formData.uby = uid
            formData.utime = tool.getTime()
            formData.lowQty = tool.xe.toNumber(formData.lowQty)
            formData.highQty = tool.xe.toNumber(formData.highQty)
            formData.price1 = formData.price1?tool.xe.toNumber(formData.price1):0
            formData.price2 = formData.price2?tool.xe.toNumber(formData.price2):0
            if(u1m) {
                formData.unit1Id = u1m.id;
            }
            if(u2m) {
                formData.unit2Id = u2m.id;
            }
            if(model.unit2Id) {
                if(!formData.unit2Id) return ctx.err('不能删除已有的单位')
            }
            if(model.unit1Id !== formData.unit1Id && ifUsed) {
                return ctx.err('已使用商品，不能修改计量单位')
            }
            if(model.unit2Id) {
                if(model.unit2Id !== formData.unit2Id  && ifUsed) {
                    return ctx.err('已使用商品，不能修改计量单位')
                }
            }
            delete formData.undefined
            await db.transaction(async () => {
                await db.models['goods'].update(formData, {
                    where: {
                        id: formData.id,
                    }
                },)
                let good = formData
                good.tenantId = tid
            })
            return ctx.suc('已更新')
        }
        else {
            formData.tenantId = tid
            formData.cby = uid
            formData.ctime = tool.getTime()
            formData.isDel = 0
            if(u1m) {
                formData.unit1Id = u1m.id;
            }
            if(u2m) {
                formData.unit2Id = u2m.id;
            }
            formData.disabled = 0
            await db.models['goods'].create(formData)
            return ctx.suc('已保存')
        }
    }
    catch (error) {
        return ctx.err(error)
    }
})



module.exports = router;
